﻿using Inovout.Component;
using Inovout.Data.Service;
using Inovout.People.Domain;
using KuaJing.LaiBa.ManageMentConsole.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KuaJing.LaiBa.ManageMentConsole.Service
{
    public class ContactLogic : RepositoryService<Contact>
    {
        PersonLogic personlogic = new PersonLogic();

        public void HandleContact()
        {
            List<Person> persons = personlogic.GetAllPerson();
            List<Persondata> dPersons = personlogic.GetALLPerson(personlogic.GetPersonData());
            List<Contact> contacts = GetAllContact();
            foreach (Person person in persons)
            {
                Persondata data = dPersons.Where(c => c.userid == person.UserId && c.name == person.Name && c.departmentid == person.OrganizationUnitId).Select(c => c).SingleOrDefault();
                if (contacts.Count == 0)
                {
                    if (!data.mobile.Equals(null) && !data.mobile.Equals(""))
                    {
                        Contact contact = new Contact();
                        contact.Value = data.mobile;
                        contact.Person = person;
                        contact.Label = ContactMethod.Mobile;
                        InsertContact(contact);
                    }
                    if (!data.phone.Equals(null) && !data.phone.Equals(""))
                    {
                        Contact contact = new Contact();
                        contact.Value = data.phone;
                        contact.Person = person;
                        contact.Label = ContactMethod.Tel;
                        InsertContact(contact);
                    }
                }
                else
                {
                    List<Contact> contactes = contacts.Where(c => c.Person == person).Select(c => c).ToList();
                    if (contacts.Count() == 0)
                    {
                        if (!data.mobile.Equals(null) && !data.mobile.Equals(""))
                        {
                            Contact contactd = new Contact();
                            contactd.Value = data.mobile;
                            contactd.Person = person;
                            contactd.Label = ContactMethod.Mobile;
                            InsertContact(contactd);
                        }
                        if (!data.phone.Equals(null) && !data.phone.Equals(""))
                        {
                            Contact contactd = new Contact();
                            contactd.Value = data.phone;
                            contactd.Person = person;
                            contactd.Label = ContactMethod.Tel;
                            InsertContact(contactd);
                        }
                    }
                    else
                    {
                        if (contacts.Count() == 2)
                        {
                            Contact contactmob = contacts.Where(c => c.Label == ContactMethod.Mobile).Select(c => c).SingleOrDefault();
                            Contact contacttel = contacts.Where(c => c.Label == ContactMethod.Tel).Select(c => c).SingleOrDefault();
                            if (!data.mobile.Equals(null) && !data.mobile.Equals(""))
                            {
                                contactmob.Value = data.mobile;
                                contactmob.Person = person;
                                InsertContact(contactmob);
                            }
                            else
                            {
                                contacts.Remove(contactmob);
                            }
                            if (!data.phone.Equals(null) && !data.phone.Equals(""))
                            {
                                contacttel.Value = data.phone;
                                contacttel.Person = person;
                                InsertContact(contacttel);
                            }
                            else
                            {
                                contacts.Remove(contacttel);
                            }
                        }
                        else
                        {
                            foreach (Contact contact in contactes)
                            {
                                contact.Person = person;
                                if (contact.Label == ContactMethod.Mobile)
                                {
                                    if (!data.mobile.Equals(null) && !data.mobile.Equals(""))
                                    {
                                        contact.Value = data.mobile;
                                        contact.Person = person;
                                        InsertContact(contact);
                                    }
                                    else
                                    {
                                        contacts.Remove(contact);
                                    }
                                }
                                else
                                {
                                    if (!data.phone.Equals(null) && !data.phone.Equals(""))
                                    {
                                        contact.Value = data.mobile;
                                        contact.Person = person;
                                        InsertContact(contact);
                                    }
                                    else
                                    {
                                        contacts.Remove(contact);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            RemovePersonContact(contacts);
        }
        /// <summary>
        /// 添加联系方式
        /// </summary>
        /// <param name="contact"></param>
        public void InsertContact(Contact contact)
        {
            IRepositoryService<Contact> Service = ComponentRegistry.Resolve<IRepositoryService<Contact>>();
            Service.Save(contact);
        }
        /// <summary>
        /// 删除联系方式
        /// </summary>
        /// <param name="person"></param>
        public void RemovePersonContact(List<Contact> contacts)
        {
            IRepositoryService<Contact> Service = ComponentRegistry.Resolve<IRepositoryService<Contact>>();
            foreach (Contact contact in contacts)
            {
                Service.Delete(contact);
            }
        }
        /// <summary>
        /// 获取联系方式
        /// </summary>
        /// <param name="person"></param>
        /// <returns></returns>
        private List<Contact> GetAllContact()
        {
            IRepositoryService<Contact> Service = ComponentRegistry.Resolve<IRepositoryService<Contact>>();
            return Service.Query().ToList();
        }
    }
}